package com.fiblue.imageserver.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import java.io.IOException;

public class HttpFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String URI = request.getRequestURI();
        String replacedUri = replaceSpecialCharacters(URI);
        HttpServletRequest wrappedRequest = new HttpServletRequestWrapper(request) {
            @Override
            public String getRequestURI() {
                return replacedUri;
            }
        };
        filterChain.doFilter(wrappedRequest, servletResponse);
    }
        private String replaceSpecialCharacters (String url){
            // 替换特殊字符的逻辑
            // 例如，可以替换 &, #, % 等字符
            return url.replace("//", "/");
        }
    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
